package main.java.com.cooljie.controller;

import com.alibaba.fastjson.JSON;
import com.cooljie.entity.Pager;
import com.cooljie.entity.Student;
import com.cooljie.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by ACER on 2017/5/24.
 */
@Controller
@RequestMapping("/students")
public class StudentInfoController {
    @Autowired
    private StudentService studentService;

    //    查询单个学生信息
    @RequestMapping(value = "/student", method = RequestMethod.GET)
    public String CheckOneStudent(Integer studentId, HttpServletRequest request) {

        if (request.getParameter("id") == null) {
            return "students/selectId";
        } else {

            Student student = studentService.findStudentById(Integer.valueOf(request.getParameter("id")));
            System.out.println(JSON.toJSONString(student) + "............>>>>>>>>>>>>>>>>>>>");
            request.setAttribute("student", student);
            return "students/student";
        }

    }

    //    删除某个学生
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String DeleteStudent(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        try {
            int studentId = Integer.parseInt(request.getParameter("id"));
            studentService.deleteById(studentId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/students/studentList";
    }

    //更新学生信息
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String updateOneStudent(HttpServletRequest request, Model model) {
        Student student = new Student();
        try {

            int id = Integer.parseInt(request.getParameter("id"));
            student.setStudentId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        student.setStudentNo(request.getParameter("sno"));
        student.setStudentName(request.getParameter("name"));
        student.setStudentPassword(request.getParameter("password"));
        student.setStudentDept(request.getParameter("dept"));
        student.setStudentSex(request.getParameter("sex"));
        model.addAttribute("student", student);
        return "students/update";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(HttpServletRequest request) {
        try {
            Student student = studentService.findStudentById(Integer.parseInt(request.getParameter("id")));
            student.setStudentNo(request.getParameter("sno"));
            student.setStudentName(request.getParameter("name"));
            student.setStudentPassword(request.getParameter("password"));
            student.setStudentDept(request.getParameter("dept"));
            student.setStudentSex(request.getParameter("sex"));
            studentService.updateStudent(student);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "redirect:/students/studentList";

    }

    //增加一个学生
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addOneStudent() {
        return "students/addStudent";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addOneStudent(@Validated Student student) throws IOException {

        studentService.insertData(student);

        return "redirect:/students/studentList";
    }

    //    分页查看所有学生信息
    @RequestMapping(value = "/studentList", method = RequestMethod.GET)
    public String showStudentList(HttpServletRequest request, Model model) {
        int page = 0;
        if (request.getParameter("pager.offset") != null) {
            page = Integer.parseInt(request.getParameter("pager.offset"));
        }

        Pager<Student> studentPager = new Pager<>();
//        设置总页数
        int allPages;
        if (studentService.allPages() % studentPager.getPageSize() == 0) {
            allPages = studentService.allPages() / studentPager.getPageSize();
        } else allPages = studentService.allPages() / studentPager.getPageSize() + 1;
        studentPager.setTotalPage(allPages);
//        总记录
        studentPager.setTotalRecord(studentService.allPages());
//        设置当前页
        int thisPage = (page + studentPager.getPageSize()) / studentPager.getPageSize();
        studentPager.setPageIndex(thisPage);


        List<Student> studentList = studentService.selectStudentByPage(page, studentPager.getPageSize());
        model.addAttribute("pager", studentPager);
        model.addAttribute("studentList", studentList);
        return "students/studentList";
    }


    //按条件查询学生信息并分页
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String searchStudent(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        String keyword = request.getParameter("keyword");

        model.addAttribute("keyword", request.getParameter("keyword"));
//        session.setAttribute("param", request.getParameter("param"));
//        System.out.println(param.toString() + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        if (keyword.length() == 0) {
            response.getWriter().write("请输入学号或者姓名");
            return null;
        }
        int page = 0;
        if (request.getParameter("pager.offset") != null) {
            page = Integer.parseInt(request.getParameter("pager.offset"));
        }

        Pager<Student> studentPager = new Pager<>();
//        设置总页数
        int allPages;
        if (studentService.allPages() % studentPager.getPageSize() == 0) {
            allPages = studentService.selectAllPages(keyword,keyword) / studentPager.getPageSize();
        } else allPages = studentService.selectAllPages(keyword,keyword) / studentPager.getPageSize() + 1;
        studentPager.setTotalPage(allPages);
//        总记录
        studentPager.setTotalRecord(studentService.selectAllPages(keyword,keyword));
//        设置当前页
        int thisPage = (page + studentPager.getPageSize()) / studentPager.getPageSize();
        studentPager.setPageIndex(thisPage);

        List<Student> studentList = studentService.search(keyword, keyword, page, studentPager.getPageSize());
        model.addAttribute("pager", studentPager);
        model.addAttribute("studentList", studentList);
        return "students/search";
    }

}
